package org.polaris.gift.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import org.polaris.gift.constants.SysConstant;
import org.polaris.gift.domain.TenantType;
import org.polaris.gift.mapper.TenantTypeMapper;
import org.polaris.gift.query.TenantTypeQuery;
import org.polaris.gift.service.ITenantTypeService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.polaris.gift.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

import static org.polaris.gift.constants.SysConstant.TENANT_TYPE;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author polaris
 * @since 2023-06-24
 */
@Service
public class TenantTypeServiceImpl extends ServiceImpl<TenantTypeMapper, TenantType> implements ITenantTypeService {

    @Autowired
    private TenantTypeMapper tenantTypeMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public PageList<TenantType> advancedQueryAndPaging(TenantTypeQuery query) {
        Object tenantType = redisTemplate.opsForValue().get(TENANT_TYPE);
        if (Objects.nonNull(tenantType)){
            return (PageList<TenantType>) tenantType;
        }
        Page<TenantType> tenantPage = new Page<>(query.getPage(), query.getRows());
        List<TenantType> data = tenantTypeMapper.advancedQueryAndPaging(tenantPage,query.getKeyword());
        PageList<TenantType> pageList = new PageList<>(tenantPage.getTotal(), data);
        redisTemplate.opsForValue().set(TENANT_TYPE, pageList);
        return pageList;
    }
}
